import request from './index'

const attachmentApi = {
  list(module, moduleId) {
    if (module === 'training-attachments') {
      return request.get(`/api/${module}/${moduleId}`)
    }
    return request.get(`/api/${module}/${moduleId}/attachments`)
  },
  upload(module, moduleId, file, description) {
    const formData = new FormData()
    formData.append('file', file)
    if (description) formData.append('description', description)
    if (module === 'training-attachments') {
      return request.post(`/api/${module}/${moduleId}`, formData, {
        headers: { 'Content-Type': 'multipart/form-data' }
      })
    }
    return request.post(`/api/${module}/${moduleId}/attachments`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },
  delete(module, moduleId, id) {
    if (module === 'training-attachments') {
      return request.delete(`/api/${module}/${moduleId}/${id}`)
    }
    return request.delete(`/api/${module}/${moduleId}/attachments/${id}`)
  },
  download(id) {
    return request.get(`/api/attachments/download/${id}`, { responseType: 'blob' })
  },
  preview(id) {
    return `/api/attachments/preview/${id}`
  }
}

export default attachmentApi 